
package com.iplant.wms.controller.pkg;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.SMSSService;
import com.iplant.wms.service.po.pkg.MSSContainer;
import com.iplant.wms.service.po.pkg.MSSContainerType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/Container")
public class ContainerController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(ContainerController.class);

	@Autowired
	SMSSService wMSSService;

	@GetMapping("/All")
	Object All(HttpServletRequest request) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			String wName = StringUtils.parseString(request.getParameter("Name"));
			String wTypeCode = StringUtils.parseString(request.getParameter("TypeCode"));
			String wTypeName = StringUtils.parseString(request.getParameter("TypeName"));

			int wTypeID = StringUtils.parseInt(request.getParameter("TypeID"),-1);

			int wActive = StringUtils.parseInt(request.getParameter("Active"),-1);
			Pagination wPagination = GetPagination(request);

			ServiceResult<List<MSSContainer>> wServerRst = wMSSService.MSS_QueryContainerList(wLoginUser, wTypeID,
					wName, wTypeCode, wTypeName, wActive, wPagination);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.Result, wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.Result, null);
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/Info")
	Object Info(HttpServletRequest request) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			int wID = StringUtils.parseInt(request.getParameter("ID"));

			String wCode = StringUtils.parseString(request.getParameter("Code"));

			ServiceResult<MSSContainer> wServerRst = wMSSService.MSS_QueryContainer(wLoginUser, wID, wCode);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst.Result);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wServerRst.Result);
			}

		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Update")
	Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSContainer wMSSContainer = CloneTool.Clone(wParam.get("data"), MSSContainer.class);
			ServiceResult<Integer> wServerRst = wMSSService.MSS_UpdateContainer(wBMSEmployee, wMSSContainer);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wMSSContainer);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), null, wMSSContainer);
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Delete")
	Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSContainer wMSSContainer = CloneTool.Clone(wParam.get("data"), MSSContainer.class);
			ServiceResult<Integer> wServerRst = new ServiceResult<Integer>(0);

			if (wMSSContainer.ID > 0) {
				wServerRst = wMSSService.MSS_DeleteContainer(wBMSEmployee, wMSSContainer);
			} else {
				wServerRst.Result = 1;
			}
			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst, null);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst, null);
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/TypeAll")
	public Object TypeAll(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);
			String wName = StringUtils.parseString(request.getParameter("Name"));
			int wActive = StringUtils.parseInt(request.getParameter("Active"),-1);
			Pagination wPagination = GetPagination(request);
			ServiceResult<List<MSSContainerType>> wServiceResult = wMSSService.MSS_QueryContainerTypeList(wLoginUser,
					wName, wActive, wPagination);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/TypeInfo")
	public Object TypeInfo(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			int wID = StringUtils.parseInt(request.getParameter("ID"));
			String wCode = StringUtils.parseString(request.getParameter("Code"));

			ServiceResult<MSSContainerType> wServiceResult = wMSSService.MSS_QueryContainerType(wLoginUser, wID, wCode);
			MSSContainerType wServerRst = wServiceResult.Result;

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
			}

		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/TypeUpdate")
	public Object TypeUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);
			int wUserID = wBMSEmployee.ID;
			String wUserName = wBMSEmployee.Name;

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSContainerType wMSSContainerType = CloneTool.Clone(wParam.get("data"), MSSContainerType.class);
			wMSSContainerType.EditorName = wUserName;
			wMSSContainerType.EditorID = wUserID;
			wMSSContainerType.EditTime = Calendar.getInstance();
			if (wMSSContainerType.ID <= 0) {

				wMSSContainerType.CreatorName = wUserName;
				wMSSContainerType.CreatorID = wUserID;
				wMSSContainerType.CreateTime = Calendar.getInstance();

			}
			ServiceResult<Integer> wServiceResult = wMSSService.MSS_UpdateContainerType(wBMSEmployee,
					wMSSContainerType);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wMSSContainerType);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/TypeDelete")
	public Object TypeDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSContainerType wMSSContainerType = CloneTool.Clone(wParam.get("data"), MSSContainerType.class);

			ServiceResult<Integer> wServiceResult = wMSSService.MSS_DeleteContainerType(wBMSEmployee,
					wMSSContainerType);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/TypeActive")
	public Object TypeActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

			List<MSSContainerType> wMSSContainerTypeList = CloneTool.CloneArray(wParam.get("data"),
					MSSContainerType.class);
			ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
			for (MSSContainerType wMSSContainerType : wMSSContainerTypeList) {

				wServiceResult = wMSSService.MSS_ActiveContainerType(wBMSEmployee, wMSSContainerType, wActive);

				if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
					break;
			}

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

}